package com.gao.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	private static String user="xiaomi";
	private static String pwd="1483851874";
	private static String db = "xiaomi";
	public static Connection getConnection(){

		String url = "jdbc:mysql://localhost:3306/"+db+"?serverTimezone=UTC&characterEncoding=utf-8";
			Connection conn=null;
			try {
				Class.forName("com.mysql.cj.jdbc.Driver");
				conn = DriverManager.getConnection(url, user, pwd);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    return conn;
	}
	
	public static  void release(PreparedStatement pstmt,Connection conn) {
		release(pstmt);
		release(conn);
	}
	
	public static  void release(ResultSet rs,PreparedStatement pstmt,Connection conn) {
		release(rs);
		release(pstmt);
		release(conn);
	}
	
	
	public static void release(ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void release(PreparedStatement pstmt) {
        if(pstmt != null) {
            try {
            	pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
	
	public static void release(Connection conn) {
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
	
}
